package com.bbs.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;
import java.sql.Date;

import com.bbs.dao.UserDao;
import com.bbs.model.Posts;
import com.bbs.model.User;
import com.bbs.util.DB;

public class UserDaoImpl implements UserDao {
	private Connection conn=null;	
    private PreparedStatement pstm=null;
    private ResultSet rs=null;
    
    
	public boolean login(String name, String password) throws Exception {
		// TODO Auto-generated method stub	
		if(conn==null){
	     conn=DB.getConnection(conn);
		}
		String sql = "select name ,passwd from user where name=? and passwd=?";
		pstm = conn.prepareStatement(sql);
		pstm.setString(1, name);
		pstm.setString(2, password);
		rs = pstm.executeQuery();
		if (rs.next()) {
			return true;
		}
		DB.getClose(rs, pstm, conn);
		return false;
	}

	public boolean addUser(String name, String password, String email) throws Exception {
		if(conn==null){
			conn=DB.getConnection(conn);
		}
		String sql = "insert into user (name,passwd,email) values (?,?,?)";
		pstm = conn.prepareStatement(sql);
		pstm.setString(1, name);
		pstm.setString(2, password);
		pstm.setString(3, email);
		if (pstm.executeUpdate() > 0) {

			return true;
		}
		DB.getClose(rs, pstm, conn);
		return false;
	}

	public boolean postRelease(Posts post) throws Exception {
		// TODO Auto-generated method stub
		
		if(conn==null){
			conn=DB.getConnection(conn);
		}
		String sql = "insert into posts(author,title,kind,content,time) values(?,?,?,?,?)";
		pstm = conn.prepareStatement(sql);
		pstm.setString(1, post.getAuthor());
		pstm.setString(2, post.getTitle());
		pstm.setString(3, post.getKind());
		pstm.setString(4, post.getContent());
		pstm.setTimestamp(5, new Timestamp(post.getTime().getTime()));

		if (pstm.executeUpdate() > 0) {
			return true;
		}
		DB.getClose(rs, pstm, conn);
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bbs.dao.UserDao#paginationUsers(int, int)
	 */
	public List<User> paginationUsers(int begin, int size) throws Exception {
		List<User> users = new ArrayList<User>();
		if (conn == null) {
			conn = DB.getConnection(conn);
		}
		String sql = "select name, passwd, email, role from user limit ?, ?";
		pstm = conn.prepareStatement(sql);
		pstm.setInt(1, begin);
		pstm.setInt(2, size);
		rs = pstm.executeQuery();
		while (rs.next()) {
			User user = new User();
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("passwd"));
			user.setEmail(rs.getString("email"));
			user.setRole(rs.getInt("role"));
			users.add(user);
		}
		DB.getClose(rs, pstm, conn);
		return users;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bbs.dao.UserDao#userTotal()
	 */
	@Override
	public int userTotal() throws SQLException {
		int userNum = 0;
		conn = DB.getConnection(conn);
		String sql = "select count(*) from user";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		if(rs.next()) {
			userNum = rs.getInt(1);
		}
		DB.getClose(rs, pstm, conn);
		return userNum;
	}

	/* (non-Javadoc)
	 * @see com.bbs.dao.UserDao#deleteUser(java.lang.String)
	 */
	@Override
	public boolean deleteUser(String name) {
		conn = DB.getConnection(conn);
		int row = 0;
		String sql = "update user set isDelete = 1 where name = ?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			row = pstm.executeUpdate();
			DB.getClose(rs, pstm, conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(row == 1) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.bbs.dao.UserDao#recoverUser(java.lang.String)
	 */
	@Override
	public boolean recoverUser(String name) {
		conn = DB.getConnection(conn);
		int row = 0;
		String sql = "update user set isDelete = 0 where name = ?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			row = pstm.executeUpdate();
			DB.getClose(rs, pstm, conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(row == 1) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.bbs.dao.UserDao#getUser(java.lang.String)
	 */
	@Override
	public User getUser(String name) {
		// TODO Auto-generated method stub
		User user = new User();
		conn = DB.getConnection(conn);
		String sql = "select name, passwd, role, email, gender, birthday, isDelete from user where name = ?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			rs = pstm.executeQuery();
			if(rs.next()) {
				user.setName(rs.getString("name"));
				user.setRole(rs.getInt("role"));
				user.setEmail(rs.getString("email"));
				user.setGender(rs.getString("gender"));
				user.setBrithday(rs.getDate("birthday"));
				user.setDelete(rs.getBoolean("isDelete"));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
